<?php
defined('PATH') or exit('Access Denied');
/**
 * 简单控制器类
 * Enter description here ...
 * @author Administrator
 *
 */
abstract class Controller{
	var $db;
	var $view;
	static public $action;
	static public $model;
	static public $_C;
    public static $licensed = false;
    public static $version;

	public static function run(){
		global $SID;
        self::$version = get_version();
        self::$licensed = self::domainCheck();
		$start_timer=microtime(1);
		$urlc=Controller::url();
		Controller::$action=ucfirst($urlc[0]);
		Controller::$model=ucfirst($urlc[1]);
		$ActionClass=Controller::$action."Action";
		$Method_Fun="cmd".Controller::$model;
		!isset($_SESSION['Themes'])?$_SESSION['Themes']=getTemplate():null;
		if (class_exists($ActionClass)){
			$Act=new $ActionClass();
			$Act->$Method_Fun();
			 
		}else{
			 new appException("没有 $ActionClass 这个控制器");
		}
		$end_timer=microtime(1);
		if (TK_DEBUG || !self::$licensed)echo "\n".'<!--页面执行时间:'.($end_timer-$start_timer).'毫秒. by bbs.8eoo.com-->';
	}
	//URL地址处理
	public static function url(){
		if (C('URL_TYPE')==1){
			return url_g();
		}elseif (C('URL_TYPE')==2){
			return url_r();
		}
	}

    public static function domainCheck(){
        /* 获取HOST */
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
        {
            $domain = $_SERVER['HTTP_X_FORWARDED_HOST'];
        }
        elseif (isset($_SERVER['HTTP_HOST']))
        {
            $domain = $_SERVER['HTTP_HOST'];
        }else{
            $domain = $_SERVER['SERVER_NAME'];
        }

        $code = md5(base64_encode(md5(md5($domain).pack('c26',97,100,107,108,103,52,117,50,51,57,97,45,48,57,119,97,111,97,61,101,119,102,111,61,115,102))));
        $license = base64_decode(SiteConfig::get('license_key'));
        $license_id = substr($license,0,6);
        $license_key = substr($license,6);


        if(empty($_SESSION['license'])){
            //$status = @file_get_contents('http://csms.sinaapp.com/license.php?domain='.$domain.'&license_id='.$license_id.'&license_key='.$license_key);
            $status = @file_get_contents(sprintf(pack('c85',104,116,116,112,58,47,47
                ,99,115,109,115,46,115,105,110,97,97
                ,112,112,46,99,111,109,47,108,105,99
                ,101,110,115,101,46,112,104,112,63,
                100,111,109,97,105,110,61,37,115,38,108,105,
                99,101,110,115,101,95,105,100,61,37,115,38,108,
                105,99,101,110,115,101,95,107,101,121,61,37,115,
                38,118,101,114,115,105,111,110,61,37,115),$domain,$license_id,$license_key,join(",",self::$version)));
            if(!empty($status)){
                if($status == 'success'){
                    $_SESSION['license'] = $status;
                }else{
                    return false;
                }
            }
        }
        return $code == $license_key;
    }
}